package com.sparkseries.controller;

import com.sparkseries.domain.vo.SysMenuVO;
import com.sparkseries.domain.vo.SysRoleVO;
import com.sparkseries.dto.SysMenuDTO;
import com.sparkseries.service.SysMenuService;
import com.sparkseries.common.util.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("menu")
@Tag(name = "菜单管理")
public class SysMenuController {
    @Resource
    private SysMenuService menuService;

    /**
     * 创建菜单
     * @param menuDTO 菜单传输类
     * @return 默认响应类
     */
    @PostMapping("createMenu")
    @Operation(summary = "创建菜单")
    @PreAuthorize("")
    public Result<?> createMenu(@RequestBody SysMenuDTO menuDTO) {
        return menuService.createMenu(menuDTO);
    }

    /**
     * 绑定菜单角色
     * @param roleIds 角色ID集合
     * @param menuId 菜单ID
     * @return 默认响应类
     */
    @PostMapping("bindMenuWithRole/{menuId}")
    @Operation(summary = "绑定菜单角色")
    @Parameter(name = "roleIds", required = true)
    @PreAuthorize("")
    public Result<?> bindMenuWithRole(@RequestParam("roleIds") List<Long> roleIds, @PathVariable("menuId") Long menuId) {
        return menuService.bindMenuWithRole(roleIds, menuId);
    }

    /**
     * 删除菜单
     * @param menuId 菜单ID
     * @return 默认响应类
     */
    @DeleteMapping("deleteMenu/{menuId}")
    @Operation(summary = "删除菜单")
    @PreAuthorize("")
    public Result<?> deleteMenu(@PathVariable("menuId") Long menuId) {
        return menuService.deleteMenu(menuId);
    }

    /**
     * 删除菜单角色绑定
     * @param roleIds 角色ID集合
     * @param menuId 菜单ID
     * @return 默认响应类
     */
    @DeleteMapping("deleteBind/{menuId}")
    @Operation(summary = "删除菜单角色绑定")
    @Parameter(name = "roleIds", required = true)
    @PreAuthorize("")
    public Result<?> deleteBind(@RequestParam("roleIds") List<Long> roleIds, @PathVariable("menuId") Long menuId) {
        return menuService.deleteBind(roleIds, menuId);
    }

    /**
     * 修改菜单
     * @param menuDTO 菜单传输类
     * @return 默认响应类
     */
    @PutMapping("updateMenu")
    @Operation(summary = "修改菜单")
    @PreAuthorize("")
    public Result<?> updateMenu(@RequestBody SysMenuDTO menuDTO) {
        return menuService.updateMenu(menuDTO);
    }

    /**
     * 修改菜单状态
     * @param menuId 菜单ID
     * @param status 是否启用（0-禁用，1-启用）
     * @return 默认响应类
     */
    @PutMapping("updateMenuStatus/{menuId}")
    @Operation(summary = "修改菜单状态")
    @Parameter(name = "status", required = true)
    @PreAuthorize("")
    public Result<?> updateMenuStatus(@PathVariable("menuId") Long menuId, @RequestParam("status") Integer status) {
        return menuService.updateMenuStatus(menuId, status);
    }

    /**
     * 获取菜单列表
     * @param menuDTO 菜单传输类
     * @return 菜单响应类集合
     */
    @GetMapping("listMenu")
    @Operation(summary = "获取菜单列表")
    @PreAuthorize("")
    public Result<List<SysMenuVO>> listMenu(@RequestBody SysMenuDTO menuDTO) {
        return menuService.listMenu(menuDTO);
    }

    /**
     * 获取菜单信息
     * @param menuId 菜单ID
     * @return 菜单响应类
     */
    @GetMapping("getMenuInfo/{menuId}")
    @Operation(summary = "获取菜单信息")
    @PreAuthorize("")
    public Result<SysMenuVO> getMenuInfo(@PathVariable("menuId") Long menuId) {
        return menuService.getMenuInfo(menuId);
    }

    /**
     * 获取菜单下的角色
     * @param menuId 菜单ID
     * @return 角色响应类集合
     */
    @GetMapping("listRoleIdsByMenuId/{menuId}")
    @Operation(summary = "获取菜单下的角色")
    @PreAuthorize("")
    public Result<List<SysRoleVO>> listRoleIdsByMenuId(@PathVariable("menuId") Long menuId) {
        return menuService.listRoleIdsByMenuId(menuId);
    }
}